package TakeAwaySystem.businessoperation.business;

import TakeAwaySystem.Business;
import TakeAwaySystem.businessoperation.BusinessIOperatition;
import TakeAwaySystem.User;

import java.io.*;
import java.util.*;

import static TakeAwaySystem.Business.*;
import static TakeAwaySystem.User.NAME;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/26 11:22:46
 */
public class ModifyBusinessOperation implements BusinessIOperatition {
    @Override
    public void work(Business o, File file) {
        System.out.println("修改当前登录的商户信息");
        o = (Business)o;
        Scanner scanner = new Scanner(System.in);
        String strT = null;
        do{
            int choice = PrintModifyOperation();
            switch(choice){
                case 0:
                    break;
                case NAME :
                {
                    List<User> userList = fileToList(file);
                    Set<String> NameSet = new HashSet<>();
                    for(User user : userList){
                        NameSet.add(user.getName());
                    }
                    String newName = null;
                    do{
                        System.out.println("请输入你要修改的 businessName.(注意名字不可以出现字符)");
                        newName = scanner.next();
                        if(newName.contains(" ")){
                            System.out.println("你设置的名字其中有空格 请重新输入");
                        }
                        if(NameSet.contains(newName)){
                            System.out.println("你设置的名字已经被其他商户使用，请选择其他名字");
                        }
                    }while(newName.contains(" ")&& NameSet.contains(newName));
                }
                break;
                case PASSWORD :
                {
                    String password = null;
                    boolean isRight = true;
                    do{
                        //直到给我收到第一次密码合格 再进行重复测试
                        do{
                            password = scanner.next();
                            char[] str = password.toCharArray();
                            isRight = str.length >= 8 ? true : false;
                            System.out.print(isRight ? "密码长度合格" :"密码长度不合格");
                            for(int i = 0;str.length >= 8 && i<str.length;i++){
                                if(!(str[i] >= '0' && str[i] <= '9') && !(str[i] >= 'a' && str[i] <= 'z') && !(str[i] >= 'A' && str[i] <= 'Z')){
                                    System.out.println("出现了除大小写字母和数字 之外的字符");
                                    isRight = false;
                                }
                            }
                            System.out.println(isRight ? "密码合格" :"密码不合格");

                        }while(!isRight);
                        System.out.println("请重复输入密码 要求和上次输入的密码相同");
                        String pass2 = scanner.next();
                        isRight = pass2.equals(password);
                        System.out.println(isRight ? "密码正确 开始注册" : " 密码错误 重新输入");
                    }while(!isRight);
                    List<User> list = fileToList(file);
                    for(User user : list){
                        if(user.getId() == ((Business) o).getId()){
                            ((Business)user).setPassword(password);
                            break;
                        }
                    }
                    ListToFile(list,file);
                }
                break;
                case ADRESS :
                {
                    String adress = null;
                    List<User> userList = fileToList(file);
                    Set<String> set = new HashSet<>();
                    for(User user : userList){
                        set.add(((Business)user).getBusinessAddress());
                    }
                    do{
                        System.out.println("请输入你要修改的地址 注意请不要和其他商家相同且地址不允许出现空格");
                        adress = scanner.next();
                        if(adress.contains(" ")){
                            System.out.println("地址出现空格 请重新输入");
                        }
                        if(set.contains(adress)){
                            System.out.println("地址和其他商家重复 请重新输入");
                        }
                    }while(adress.contains(" ") && set.contains(adress));
                    for(User user : userList){
                        if(((Business)user).getId() == ((Business) o).getId()){
                            ((Business) user).setBusinessAddress(adress);
                            break;
                        }
                    }
                    ListToFile(userList,file);

                }
                System.out.println("修改成功");
                break;
                case EXPLAIN :
                {
                    String explain = null;
                    List<User> userList = fileToList(file);
                    do{
                        System.out.println("请输入你要修改的解释 注意不允许出现空格");
                        explain = scanner.next();
                        if(explain.contains(" ")){
                            System.out.println("解释出现空格 请重新输入");
                        }

                    }while(explain.contains(" "));
                    for(User user : userList){
                        if(((Business)user).getId() == ((Business) o).getId()){
                            ((Business)user).setBusinessExplain(explain);
                            break;
                        }
                    }
                    ListToFile(userList,file);
                }
                System.out.println("修改成功");
                break;
                case STARPRICE:
                {
                    double newStartPrice = 0;
                    String newPrice = null;
                    boolean isRight = true;
                    do{
                        System.out.println("请输入你要修改的起送价格");
                        newPrice = scanner.next();
                        int starPoint = newPrice.indexOf('.');
                        int endPoint = newPrice.lastIndexOf('.');
                        if(starPoint != endPoint){
                            System.out.println("小数点出现两个");
                            isRight = false;
                        }
                        char[] str = newPrice.toCharArray();
                        for(int i = 0;starPoint != endPoint && i< str.length;i++){
                            if(!(str[i] >= '0' && str[i] <= '9') && str[i] != '.'){
                                isRight = false;
                                System.out.println("输入的数据只能包含数字和小数点 请重新输入");
                            }
                        }
                    }while(!isRight);
                    newStartPrice = Double.valueOf(newPrice);
                    List<User> userList = fileToList(file);
                    for(User user : userList){
                        if(((Business)user).getId() == ((Business)o).getId()){
                            ((Business)user).setStarPrice(newStartPrice);
                        }
                    }
                    ListToFile(userList,file);
                }
                System.out.println("修改成功");
                break;
                case DELIVERPRICE:
                {
                    double newDeliverprice = 0;
                    String newPrice = null;
                    boolean isRight = true;
                    do{
                        System.out.println("请输入你要的运费");
                        newPrice = scanner.next();
                        int starPoint = newPrice.indexOf('.');
                        int endPoint = newPrice.lastIndexOf('.');
                        if(starPoint != endPoint){
                            System.out.println("小数点出现两个");
                            isRight = false;
                        }
                        char[] str = newPrice.toCharArray();
                        for(int i = 0;starPoint != endPoint && i< str.length;i++){
                            if(!(str[i] >= '0' && str[i] <= '9') && str[i] != '.'){
                                isRight = false;
                                System.out.println("输入的数据只能包含数字和小数点 请重新输入");
                            }
                        }
                    }while(!isRight);
                    newDeliverprice = Double.valueOf(newPrice);
                    List<User> userList = fileToList(file);
                    for(User user : userList){
                        if(((Business)user).getId() == ((Business)o).getId()){
                            ((Business)user).setDeliveryPrice(newDeliverprice);
                        }
                    }
                    ListToFile(userList,file);
                }
                System.out.println("修改成功");
                break;
                default:
                    System.out.println("输入错误 请重新输入");
                    break;
            }
            System.out.println("请问你现在还需要继续修改么? 如果需要请输入YES 否则输入NO");
            strT = scanner.next();
        }while (strT.equals("YES"));
        System.out.println("现在退出");
    }
    private int PrintModifyOperation(){
        System.out.println("请输入你要修改的选项的数字");
        Scanner scanner = new Scanner(System.in);
        System.out.println("0.退出");
        System.out.println("1.business NAME");
        System.out.println("2.business PASSWORD");
        System.out.println("3.business ADRESS");
        System.out.println("4.business EXPLAIN");
        System.out.println("5.business STARPRICE");
        System.out.println("6.business DELIVERPRICE");
        System.out.println("请输入相应的数字");
        return scanner.nextInt();
    }

    @Override
    public void ListToFile(List<User> list,File file) {
       // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for(int i = 0;i<N;i++){
                Business user = (Business) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(user.getId() + " ");
                sbr.append(user.getPassword() + " ");
                sbr.append(user.getName() + " ");
                sbr.append(user.getBusinessAddress() + " ");
                sbr.append(user.getBusinessExplain() + " ");
                sbr.append(user.getStarPrice() + " ");
                sbr.append(user.getDeliveryPrice());
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }


    @Override
    public List<User> fileToList(File file)  {
        List<User> list = new ArrayList<>();
       // File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while(true){
                String str = br.readLine();
                if(str != null){
                    String[] arr = str.split(" ");
                    Business users = new Business();
                    users.setId(Integer.valueOf(arr[0]));
                    users.setPassword(arr[1]);
                    users.setName(arr[2]);
                    users.setBusinessAddress(arr[3]);
                    users.setBusinessExplain(arr[4]);
                    users.setStarPrice(Double.valueOf(arr[5]));
                    users.setDeliveryPrice(Double.valueOf(arr[6]));
                    list.add(users);
                }else{
                    break;
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }

}
